package main

import (
	"fmt"
	"io/ioutil"
	"strings"
	"strconv"
	"github.com/360EntSecGroup-Skylar/excelize"
)

func main() {
	// var names,_ = getAllFile(".")
	var excelContents,_=getAllFile(".")
	f := excelize.NewFile()
	colStyle,err:=f.NewStyle(`{"Alignment": {"Horizontal":"center","Vertical":"center"}}`)
	f.SetColStyle("Sheet1", "A:E", colStyle)
	exp := "0.000"
	cellStyle,err:=f.NewStyle(&excelize.Style{CustomNumFmt: &exp})
	if err != nil {
		println(err.Error())
	}
    // 设置工作簿的默认工作表
    // 创建一个工作表
    // index := f.NewSheet("Sheet2")
    // 设置单元格的值
	// f.SetCellValue("Sheet2", "A2", "Hello world.")
	f.MergeCell("Sheet1", "A1", "E2")
	f.MergeCell("Sheet1", "A3", "A4")
	f.MergeCell("Sheet1", "B3", "B4")
	f.MergeCell("Sheet1", "C3", "C4")
	f.MergeCell("Sheet1", "D3", "E3")
	f.SetCellValue("Sheet1", "A1", "界 址 点 成 果 表")
	f.SetCellValue("Sheet1", "A3", "图斑号")
    f.SetCellValue("Sheet1", "B3", "序号")
    f.SetCellValue("Sheet1", "C3", "点号")
    f.SetCellValue("Sheet1", "D3", "坐标")
    f.SetCellValue("Sheet1", "D4", "x(m)")
	f.SetCellValue("Sheet1", "E4", "y(m)")
	f.SetColWidth("Sheet1", "A", "C", 9)
	f.SetColWidth("Sheet1", "D", "E", 16)
	// f.SetActiveSheet(index)
	var ros=0
	var ross=0
	for _,rowsData := range excelContents{
		
		ross=ros
		for i,rowData := range rowsData{
			for j,colCell := range rowData{
				cellName,_:=excelize.CoordinatesToCellName(j+2,i+ross+5)
				if(j>1){
					floatColCell, _:= strconv.ParseFloat(colCell, 64)
					f.SetCellValue("Sheet1", cellName, floatColCell)
					f.SetCellStyle("Sheet1", cellName,cellName,cellStyle)
				}else{
					f.SetCellValue("Sheet1", cellName, colCell)
				}
			}
			ros=ros+1
			if(ros!=0){
				// fmt.Println(ross)
				// fmt.Println(ros)
				cellName1,_:=excelize.CoordinatesToCellName(1,ross+5)
				cellName2,_:=excelize.CoordinatesToCellName(1,ros+4)
				f.MergeCell("Sheet1", cellName1, cellName2)
			}
		}
	}

	var excelName string
	fmt.Println("合并后保存为(取个漂亮的文件名)：")
	fmt.Scanln(&excelName)//读取键盘的输入，通过操作地址，赋值给x和y   阻塞式

	// 根据指定路径保存文件
	if err := f.SaveAs(excelName+".xlsx"); err != nil {
        println(err.Error())
	}
}

func getExcelContent(fileName string)([][]string){
	var rowsData [][]string

	f, err := excelize.OpenFile(fileName)
    if err != nil {
        println(err.Error())
        // return
    }
    // // 获取工作表中指定单元格的值
    // cell, err := f.GetCellValue("Sheet1", "B2")
    // if err != nil {
    //     println(err.Error())
    //     return
    // }
    // println(cell)
	// 获取 Sheet1 上所有单元格
	//f.SetActiveSheet(0)
	rows, err := f.GetRows(f.GetSheetName(0))
	
    for i, row := range rows {
		var rowData []string
		if(i>8&&i<80){
			if(i%2==1){
				for _, colCell := range row {
					if(colCell !=""){
						rowData=append(rowData,colCell)
						//fmt.Println(colCell)
					}
				}
				if(rowData!=nil){
					rowsData=append(rowsData,rowData)
				}
			}
		}else if(i>89&&i<163){
			if(i%2==0){
				for _,colCell := range row {
					if(colCell !=""){
						rowData=append(rowData,colCell)
						//fmt.Println(colCell)
					}
				}
				if(rowData!=nil){
					rowsData=append(rowsData,rowData)
				}
							}		
		}else if(i>172&&i<246){
			if(i%2==1){
				for _,colCell := range row {
					if(colCell !=""){
						rowData=append(rowData,colCell)
						// fmt.Println(colCell)
					}
				}
				if(rowData!=nil){
					rowsData=append(rowsData,rowData)
				}
							}
		}else{
			// fmt.Println("000")
		}
	}
	return rowsData
}


func getAllFile(pathname string) ([][][]string,error) {
	var names []string
	var contents [][]string
	rd, err := ioutil.ReadDir(pathname)
	


	var excelContents [][][]string
    for _, fi := range rd {
        if fi.IsDir() {
            //fmt.Printf("[%s]\n", pathname+"\\"+fi.Name())
            //GetAllFile(pathname + fi.Name() + "\\")
        } else {
			if strings.Index(fi.Name(), ".xlsx")>0{
				names=append(names, fi.Name())
				contents=getExcelContent(fi.Name())
				//imageResize(fi.Name())
				excelContents=append(excelContents,contents)
			}
        }
	}
	//fmt.Println(excelContents)


	//fmt.Println(names,len(names))
    return excelContents,err
}